package com.zhike.util;

import java.util.ArrayList;
import java.util.List;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONArray;
import com.google.gson.Gson;
@SuppressWarnings("all")
@Component
@Scope(scopeName="prototype")
public class JsonUtil {
	private boolean success = true;
	private int errorCode = CommonUtil.ERROR_SUCCESS;
	private String msg = "调用接口成功";
	private Body body = new Body();
	/**
	 * 构建空的正确的json
	 */
	public static String blank(boolean success,int errorCode,String msg){
		JsonUtil ju = new JsonUtil(success, errorCode, msg);
		Gson gson = new Gson();
		return gson.toJson(ju);
	}
	
	public void add(Object obj){
		if(obj!=null){
			this.getBody().getList().clear();
			this.getBody().getList().add(obj);
		}
	}
	/**
	 * JSON转list
	 * @param jsonString
	 * @param clazz
	 * @return
	 */
	  public static <T> List<T> jsonToList(String jsonString, Class<T> clazz) {
	        @SuppressWarnings("unchecked")
	        List<T> ts = (List<T>) JSONArray.parseArray(jsonString, clazz);
	        return ts;
	    }
	  
	public JsonUtil() {
		super();
	}
	
	public JsonUtil(boolean success, int errorCode, String msg) {
		super();
		this.success = success;
		this.errorCode = errorCode;
		this.msg = msg;
	}
	
	public JsonUtil(boolean success, int errorCode, String msg, Body body) {
		super();
		this.success = success;
		this.errorCode = errorCode;
		this.msg = msg;
		this.body = body;
	}
	
	public boolean isSuccess() {
		return success;
	}
	public void setSuccess(boolean success) {
		this.success = success;
	}
	public int getErrorCode() {
		return errorCode;
	}
	public void setErrorCode(int errorCode) {
		this.errorCode = errorCode;
	}
	public String getMsg() {
		return msg;
	}
	public void setMsg(String msg) {
		this.msg = msg;
	}
	public Body getBody() {
		return body;
	}
	public void setBody(Body body) {
		this.body = body;
	}
	public List getList(){
		body.flush();
		return body.getList();
	} 
	
	public void setList(List list){
		body.setList(list);
	}
	public void setTotal(Long total){
		body.setTotal(new Long(total).intValue() );
	}
	public void setTotal(int total){
		body.setTotal(total);
	}
	public String getJson(){
		Gson gson = new Gson();
		this.body.setCurrentTime(DateUtil.getCurrentTime());
		this.body.setTimestamp(DateUtil.getTimeStramp());
		return gson.toJson(this);
	}

	public void setPageInfo(int pageNum, long total,int pageTotal) {
		body.setPage(pageNum);
		body.setTotal(new Long(total).intValue());
		body.setPageTotal(pageTotal);
	}
	
	public void setPageInfo(int pageNum, int total,int pageTotal) {
		body.setPage(pageNum);
		body.setTotal(total);
		body.setPageTotal(pageTotal);
	}

	public void setPageInfo(int pageNum, int total) {
		body.setPage(pageNum);
		body.setTotal(total);
		
	}
}
@SuppressWarnings("all")
class Body{
	private int page=1;//当前page
	private int total = 1;//总记录数
	private int pageTotal = 1;//总页数
	private String currentTime=DateUtil.getCurrentTime();
	private Long timestamp=DateUtil.getTimeStramp();
	private List list = new ArrayList();
	

	public int getPageTotal() {
		return pageTotal;
	}
	public void setPageTotal(int pageTotal) {
		this.pageTotal = pageTotal;
	}
	public void flush(){
		list.clear();
	}
	public int getPage() {
		return page;
	}
	public void setPage(int page) {
		this.page = page;
	}
	public int getTotal() {
		return total;
	}
	public void setTotal(int total) {
		this.total = total;
	}
	public List getList() {
		return list;
	}
	public void setList(List list) {
		this.list = list;
	}
	public String getCurrentTime() {
		return currentTime;
	}
	public void setCurrentTime(String currentTime) {
		this.currentTime = currentTime;
	}
	public long getTimestamp() {
		return timestamp;
	}
	public void setTimestamp(Long timestamp) {
		this.timestamp = timestamp;
	}
}

